package com.gadgets.framework.dict.init;

import com.gadgets.framework.common.dto.DictQueryDTO;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.pojo.Dict;
import com.gadgets.framework.common.util.CacheUtils;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.dict.service.IDictQueryService;
import com.gadgets.framework.start.AbsCacheInitLoaderProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author xiujunqing
 * @version V1.0.1
 * @data 2021-09-22
 * @moduel com.gadgets.framework.dict.init
 */
@Component
@Transactional(transactionManager = "gadgetsDbTransactionManage")
public class DictStartInitLoader extends AbsCacheInitLoaderProcess {

    @Autowired
    @Qualifier(value = "dictQueryServiceImpl")
    IDictQueryService dictQueryService;
    @Autowired
    CacheUtils cacheUtils;

    public DictStartInitLoader() {
        super("3");
    }

    @Override
    public void initLoad() {
        // 构建查询条件
        int currentPage = 0;
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(11);
        DictQueryDTO queryDTO = new DictQueryDTO();
        queryDTO.addAsc("GROUP_CODE");
        queryDTO.addAsc("ID");
        LinkedHashMap<String,HashMap<String,String>> dictCacheMap = new LinkedHashMap<>();
        // 当总页数大于当前页数时，再次查询
        while (currentPage==0 || pagination.getTotalPage()>currentPage){
            pagination.setCurrentPage(++currentPage);
            PaginationUtil.bulidPaginationInfo(pagination);
            List<Dict> resultData = dictQueryService.pageQueryDictInfo(queryDTO,pagination);
            // 循环将数据加入到Map中
            if(null!=resultData){
                for (Dict dict : resultData) {
                    HashMap<String,String> groupCodeMap = dictCacheMap.get(dict.getGroupCode());
                    //noinspection Java8MapApi
                    if(null==groupCodeMap){
                        groupCodeMap = new HashMap<>();
                        dictCacheMap.put(dict.getGroupCode(),groupCodeMap);
                    }
                    groupCodeMap.put(dict.getKey(),dict.getValue());
                }
                // 将全部字典加入到缓存中
                int count = 1;
                for (Iterator<Map.Entry<String, HashMap<String, String>>> it = dictCacheMap.entrySet().iterator(); it.hasNext();) {
                    Map.Entry<String, HashMap<String, String>> entry = it.next();
                    // 因为最后一个Key可能在下一页还有数据，因此需要跳过最后一个，由下一次遍历提交
                    if(count==dictCacheMap.size()){
                        continue;
                    }
                    cacheUtils.putDictParamCache(entry.getKey(),entry.getValue());
                    it.remove();
                }
            }
        }
        // 把最后剩余的字典全部加入到缓存里面去
        for (Map.Entry<String, HashMap<String, String>> entry : dictCacheMap.entrySet()) {
            cacheUtils.putDictParamCache(entry.getKey(),entry.getValue());
        }
    }

}
